/*
 * 作者：Peter Xiang
 * 联系方式：565067150@qq.com
 * 文档: https://github.com/PxGame
 * 创建时间: 2019/10/28 16:37:28
 */

using System;
using UnityEditor;
using UnityEngine;
using ActionSystem.Core;
using System.Reflection;

namespace XMLib.AM
{
    /// <summary>
    /// StateSetView：当前状态具体设置
    /// </summary>
    [Serializable]
    public class StateSetView : IDataView
    {
        public override string title => "状态设置";
        public override bool useAre => true;

        private Vector2 scrollView = Vector2.zero;

        protected override void OnGUI(Rect rect)
        {
            StateItem config = win.currentState;
            if (null == config)
            {
                return;
            }

            scrollView = EditorGUILayout.BeginScrollView(scrollView);

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("所属状态组",GUILayout.MaxWidth(80));
            config.GID = EditorGUILayout.IntPopup(config.GID, win.GetGroupNames().ToArray(),win.GetGroupGID().ToArray());
            EditorGUILayout.EndHorizontal();
            Type type = config.GetType();
            var fields = type.GetFields();
            FieldInfo stateField = null;

            for(int i=0;i<fields.Length;i++)
            {
                if(fields[i].GetCustomAttribute<BaseStateTypesAttribute>()!=null)
                {
                    stateField = fields[i];
                    break;
                }
            }
            if(stateField!=null)
            {
                EditorGUILayoutEx.DrawField(config, stateField);
            }
            if (EditorGUI.EndChangeCheck())
            {
                win.actionMachineDirty = true;
            }

            EditorGUILayout.EndScrollView();
        }

        public override void OnUpdate()
        {
        }

        public override object CopyData()
        {
            return win.currentState;
        }

        public override void PasteData(object data)
        {
            if (win.currentState != null && data is StateItem state)
            {
                win.currentStates[win.stateSelectIndex] = state;
            }
        }
    }
}